define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, GoPage, GoDialog, FileSystem, GoPicture) {

		//文件列表
		$scope.files = new Array();
		//$scope.files = [{ name: '新品测试最长最终_新包装最长最终_文件名称最长最终最长最终.xls' }, { name: '最新文件2.xls' }];
		$scope.allSelecr = false;

		//页面激活
		$scope.$on('$ionicView.afterEnter', function() {
			GoPage.init_top($scope, 0, 0 + 44);
			$scope.initfile();
		}, false);
		//加载文件系统
		$scope.initfile = function() {
			//根目录
			FileSystem.readFileSystemRoot(0,function(data) {
				$scope.files = new Array();
				for(var i = 0; i < data.length; i++) {
					if(data[i].isFile)
						$scope.files.push(data[i]);
				}
			}, function(e) {
				GoDialog.alert('提示', '获取列表失败');
			});
		};
		//打开文件
		$scope.openfile = function(item) {
			var type = item.name.split('.');
			type = type[type.length-1];
			if(type == 'doc'||type=='docx'){
				type = 'application/msword';
			}else if( type =='xls'||type=='xlsx'){
				type = 'application/vnd.ms-excel';
			}
			FileSystem.fileOpenWithDialog(item.nativeURL,type, function() {
				GoDialog.alert('提示', '文件打开成功');
			}, function(e) {
				GoDialog.alert('提示', '文件打开失败');
			});
		};
		//全选
		$scope.doSelectFile = function() {
			for(var i = 0; i < $scope.files.length; i++) {
				$scope.files[i].check = !$scope.allSelecr;
			}
			$scope.allSelecr = !$scope.allSelecr;
		};
		//获取选中文件
		$scope.selectFile = function() {
			var items = new Array();
			for(var i = 0; i < $scope.files.length; i++) {
				if($scope.files[i].check == true)
					items.push($scope.files[i].nativeURL);
			}
			return items;
		};
		//删除指定文件
		$scope.doDeleteFile = function() {
			var delitems = $scope.selectFile();
			if(delitems.length == 0) {
				GoDialog.alert('提示', '请选择文件');
			} else {
				//包含删除所有文件
				FileSystem.deleteFileSystemRoot(delitems, function() {
					//重新加载文件
					$scope.initfile();
					GoDialog.alert('提示', '文件删除成功');
				});
			}
		};
	}

	controller.$inject = ['$scope', 'GoPage', 'GoDialog', 'FileSystem', 'GoPicture'];

	return app.registerController("filesystemctrl", controller);
});